package com.daafan.minilib.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.daafan.minilib.activity.BookListActivity;
import com.daafan.minilib.activity.BookListActivity.ProgressReciever;
import com.daafan.minilib.data.BookData;
import com.daafan.minilib.data.BookList;
import com.daafan.minilib.util.AppUtils;

public class BookListService extends Service {

	// 定义onBinder方法所返回的对象
	private BookBinder											binder			= new BookBinder();
	private Map<String, net.tsz.afinal.http.HttpHandler<File>>	handlerMap		= new HashMap<String, net.tsz.afinal.http.HttpHandler<File>>();
	private Map<String, BookData>								bookMap			= new HashMap<String, BookData>();

	private Map<String, AjaxCallBack<File>>						AjaxCallBackMap	= new HashMap<String, AjaxCallBack<File>>();

	// 通过继承Binder来实现IBinder类
	public class BookBinder extends Binder {
		public BookListService getService() {
			return BookListService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	// 应该用Broadcast
	public void download(final String bookId) {

		net.tsz.afinal.http.HttpHandler<File> handler = handlerMap.get(bookId); // 以book为索引

		FinalHttp fh = new FinalHttp();
		final String filePath = getAPKName(bookId);

		BookData book = bookMap.get(bookId);

		if (book != null)
			if (handler == null || (handler != null && (handler.isStop() || handler.isCancelled()))) {
				handler = fh.download(book.pdfurl, filePath, true, new AjaxCallBack<File>() {

					@Override
					public void onStart() {
						BookData tbook = bookMap.get(bookId);
						tbook.isDownloading = true;
					}

					@Override
					public void onLoading(long count, long current) {
						BookData tbook = bookMap.get(bookId);
						tbook.countBit = count;
						tbook.current = current;

						tbook.update(getApplicationContext());

						// 进度
						double progress = (double) current * 100 / count;

						Intent intent = new Intent(BookListActivity.ProgressReciever.ACTION_PROGRESS);
						intent.putExtra(ProgressReciever.BOOKID, bookId);
						intent.putExtra(ProgressReciever.PROGRESS, progress);
						sendBroadcast(intent);
					}

					@Override
					public void onSuccess(File t) {
						BookData tbook = bookMap.get(bookId);
						tbook.current = tbook.countBit;
						tbook.isDownloading = false;
						tbook.isFinish = true;
						tbook.setPdfPath(filePath);

						tbook.update(getApplicationContext());

						Intent intent = new Intent(BookListActivity.ProgressReciever.ACTION_SUCCESS);
						intent.putExtra(ProgressReciever.BOOKID, bookId);
						sendBroadcast(intent);
					}
				});
				handlerMap.put(bookId, handler);
			}
	}

	public void setBookList(List<BookData> list) {
		for (BookData book : list) {
			bookMap.put(book.getBookid(), book);
		}
	}

	public void stopDownload(String bookId) {
		net.tsz.afinal.http.HttpHandler<File> handler = handlerMap.get(bookId);
		handler.stop();

		BookData tbook = bookMap.get(bookId);
		tbook.isDownloading = false;
	}

	/** 放入callBack，让本来下载book的线程，可以获取到callBack */
	public void putCallBack(BookData book, AjaxCallBack<File> callBack) {
		AjaxCallBackMap.put(book.bookid, callBack);
	}

	public boolean isDownloading(String bookId) {
		BookData tbook = bookMap.get(bookId);
		if (tbook != null)
			return tbook.isDownloading;
		else {
			return false;
		}
	}

	public static String getAPKName(String bookId) {
		return AppUtils.getMyCacheDir("pdf") + "book_" + bookId + ".pdf";
	}
}
